#ifndef LOG_H
#define LOG_H
#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>

#define LOG_VERSION "0.1.0"
#define filename(x) strrchr(x,'/')?strrchr(x,'/')+1:x

typedef struct {
  va_list ap;
  const char *fmt;
  const char *file;
  struct tm *time;
  void *udata;
  int line;
  int level;
} log_Event;

typedef void (*log_LogFn)(log_Event *ev);
typedef void (*log_LockFn)(bool lock, void *udata);

enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };

#define log_trace(...) log_log(LOG_TRACE, filename( __FILE__ ), __LINE__, __VA_ARGS__)
#define log_debug(...) log_log(LOG_DEBUG, filename( __FILE__ ), __LINE__, __VA_ARGS__)
#define log_info(...)  log_log(LOG_INFO,  filename( __FILE__ ), __LINE__, __VA_ARGS__)
#define log_warn(...)  log_log(LOG_WARN,  filename( __FILE__ ), __LINE__, __VA_ARGS__)
#define log_error(...) log_log(LOG_ERROR, filename( __FILE__ ), __LINE__, __VA_ARGS__)
#define log_fatal(...) log_log(LOG_FATAL, filename( __FILE__ ), __LINE__, __VA_ARGS__)

#define log_debug_q(quiet, ...) do { if (!quiet) log_debug(__VA_ARGS__); } while (0);
#define log_info_q(quiet, ...) do { if (!quiet) log_info(__VA_ARGS__); } while (0);

const char* log_level_string(int level);
void log_set_lock(log_LockFn fn, void *udata);
void log_set_level(int level);
void log_set_quiet(bool enable);
int log_add_callback(log_LogFn fn, void *udata, int level);
int log_add_fp(FILE *fp, int level);

void log_log(int level, const char *file, int line, const char *fmt, ...);

#ifdef __cplusplus
}
#endif
#endif
